<template>
  <div>
    <div
      v-for="item in users"
      :key="item.userId"
      @click="toUser(item.userId)"
      class="each-user"
    >
      <div class="each-user-left">
        <img v-lazy="item.avatarUrl" />
      </div>
      <div class="each-user-right">
        <h3 class="each-user-name">
          {{ item.nickname }}
          <span>{{ item.gender }}</span>
        </h3>
        <p class="each-user-msg">{{ item.signature }}</p>
      </div>
    </div>
    <mu-infinite-scroll
      :loadingText="loadingText"
      :loading="loading"
      @load="fetchData"
    />
  </div>
</template>

<script lang="ts">
import { Vue, Component, Watch } from "vue-property-decorator";
import { UserInfo } from "@/model/music-info.model";
import Router, { Route } from "vue-router";
import { APIType } from "@/api";
import { SearchType } from "@/model/search-response.model";
import { CommonIdType } from "@/model/id-or-enum.model";
@Component({})
export default class SearchResultUserComponent extends Vue {
  users: UserInfo[] = [];
  loading = false;
  loadingText = "努力加载中...";

  $route: Route;
  $router: Router;
  $api: APIType;

  mounted() {
    this.fetchData();
  }
  // 计算当前搜索的值
  get query() {
    return this.$route.query.val;
  }
  // query变化重新搜索
  @Watch("query")
  private onQueryChange() {
    this.users = [];
    this.fetchData();
  }
  toUser(id: CommonIdType) {
    this.$router.push({ path: `/userHome/${id}` }).catch((err) => err);
  }
  fetchData() {
    const offset = this.users.length;
    this.loading = true;
    this.$api
      .searchByType<"User">(this.query, SearchType.User, offset)
      .then((res) => {
        if (res?.result?.userprofiles?.length) {
          this.users = [...this.users, ...res.result.userprofiles];
        }
      })
      .finally(() => (this.loading = false));
  }
}
</script>

<style lang="scss" scoped>
.each-user {
  display: flex;
  .each-user-left {
    width: 1rem;
    padding: 0.1rem;
    img {
      width: 100%;
      border-radius: 50%;
    }
  }
  .each-user-right {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    border-bottom: 1px solid #f9fafc;
    .each-user-name {
      font-size: 0.2rem;
      font-weight: 400;
    }
    .each-user-msg {
      font-size: 0.15rem;
      color: #666;
    }
  }
}
</style>
